package com.itpan.interceptors;

import com.itpan.Utils.JwtUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class LoginInterceptor implements HandlerInterceptor {

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String token=request.getHeader("Authorization");
////
//        if (JwtUtils.verify(token)) {
//            System.out.println("拦截器：接收到你的请求啦");
//            return true;
//        }
//        System.out.println("拦截器：接收到你的请求啦，但是未登入呢");
//        response.setStatus(401);
//        return false;
////        return true;
//
//   }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String authorizationHeader = request.getHeader("Authorization");

        String token = null;
        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            token = authorizationHeader.substring(7); // 去掉Bearer后的空格，获取真正的token部分
        }

        if (token != null && JwtUtils.verify(token)) {
            System.out.println("拦截器：接收到你的请求啦");
            return true;
        }

        System.out.println("拦截器：接收到你的请求啦，但是未登入呢");
        response.setStatus(401);
        return false;
//        return true;
   }

}
